<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function index()
    {
        $class = M('class');
        $ret   = $class->select();
        $cou   = $class->count();
        for ($i = 0; $i < 3; $i++) {
             $num   = rand(1, $cou);
            foreach ($ret as $key => $value) {
                if ($value['id'] == $num) {
                    $res[] = $this->getsearch($value['name'], 20);
                }
            }
        }
        $arr = array_merge_rec($res);
        $this->assign('data', $arr);
        $this->display();
    }

    public function show()
    {
        $id = I('get.id');
        if (!$id) {
            $this->redirect('Index/index');
        }
        $Model = D('Goods');
        $data  = $Model->find($id);
        // 取出类似商品
        $gs    = $data['goodsclass'];
        $id    = $data['id'];
        $today = date('Y-m-d');
        $goods = $Model->getClassGoods($gs, $id, $today);

        $this->assign(array(
            'data'  => $data,
            'goods' => $goods,
            'title' => ' - ' . $data['goodsname'],
        ));
        $this->display();
    }

    /**精选优品*/
    public function group()
    {
        $tbk   = new \Org\Tbk\Tbk;
        $field = "item_url,pict_url,reserve_price,title,zk_final_price,provcity,nick";
        $resid = $tbk->getUatmFavoritesGetRequest();

        $res = $resid['results']['tbk_favorites'];
        $rid = $res[0]['favorites_id']; //指定默认的id号

        $id = I('get.id');
        if (!$id) {
            $id = $rid;
        }

        $arr = array_merge($res);
        foreach ($arr as $key => $value) {
            if ($id == $value['favorites_id']) {
                $rtitle = $value['favorites_title'];
            }
        }

        // $data = S('ItemGetRequest' . $id);
        if (!$data) {
            $ret  = $tbk->getUatmFavoritesItemGetRequest($id, $field);
            $data = $ret['results']['uatm_tbk_item'];
            S('ItemGetRequest' . $id, $data, 7200);
        }
        $this->assign(array(
            'data'   => $data,
            'rtitle' => $rtitle,
            'fovid'  => $id,
        ));
        $this->display();
    }

    /** 淘抢购 */
    public function rush()
    {
        // $data = S('getJuTqgGetRequest');
        if (!$data) {
            $tbk    = new \Org\Tbk\Tbk;
            $fields = 'category_name,pic_url,title,zk_final_price,reserve_price,click_url,start_time';
            $res    = $tbk->getJuTqgGetRequest($data, $fields);
            $data   = $res['results']['results'];
            S('getJuTqgGetRequest', $data, 7200);
        }
        $this->assign(array(
            'data' => $data,
        ));
        $this->display();
    }

    public function search()
    {
        if (!$sr) {
            $goodsname = I('get.sr');
        } else {
            $goodsname = $sr;
        }

        $data = $this->getsearch($goodsname);
        $this->assign(array(
            'sr'   => $goodsname,
            'data' => $data,
        ));

    }

    /**商品搜索*/
    public function getsearch($goodsname, $limit)
    {
        $tbk    = new \Org\Tbk\Tbk;
        $fields = 'num_iid,nick,pict_url,title,,zk_final_price,reserve_price,item_url';
        $ret    = $tbk->getGoodsRequest($goodsname, $fields, $limit);
        $data   = $ret['results']['n_tbk_item'];
        return $data;

    }

}
